package com.cnfol.emoj.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import net.tsz.afinal.json.JsonMap;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.adapter.MyBaseAdapter;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;

import java.util.List;
import java.util.Map;

public class EmotionGridViewAdapter extends MyBaseAdapter {

	public EmotionGridViewAdapter(Context context) {
		super(context);
	}

	public EmotionGridViewAdapter(Context context,
                                  List<? extends Map<String, ?>> datas) {
		super(context, datas);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		JsonMap jsonMap = (JsonMap) getItem(position);
		int itemWidth = getWindowPX();
		int padding = itemWidth / 6;
		ImageView iv = new ImageView(context);
		LayoutParams params = new LayoutParams(itemWidth, itemWidth);
		iv.setLayoutParams(params);
		iv.setScaleType(ScaleType.FIT_CENTER);
		iv.setPadding(padding, 0, padding, 0);
		ImageLoader.getInstance().displayImage(jsonMap.getStringNoNull("url"),
				iv, MyApplication.getInstance().getDefaultOption(),
				new ImageLoaderBitmapCallBack());
		return iv;

	}

	private int getWindowPX() {
		Activity activity;
		if (context instanceof Activity) {
			activity = (Activity) context;
			DisplayMetrics dm = new DisplayMetrics();
			activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
			int width = dm.widthPixels;
			return width / 8;
		} else {
			return 200;
		}
	}

}
